//
//  ImageViewerVC.swift
//  QZWM_User_new
//
//  Created by youai on 2019/5/16.
//  Copyright © 2019年 youi. All rights reserved.
//

import Foundation
import UIKit
class ImageViewer : UIView,UIScrollViewDelegate {
    
    let imageBGView = UIView()
    let scrollView = UIScrollView()
    
    var imageRect = CGRect(x: 0, y: 0, width: 1, height: 1)
    
    init(vc:BaseViewController, image:UIImage) {
        super.init(frame:CGRect(origin: .zero, size: vc.view.bounds.size))
        
        self.backgroundColor = .init(white:0.0,alpha:0.4)
        
        vc.view.addSubview(self)
        scrollView .frame = vc.safeView.frame
        addSubview(scrollView)
        scrollView.delegate = self
        
        let bSize = scrollView.frame.size
        
        imageBGView.frame = CGRect(origin: .zero, size: bSize)
        scrollView.addSubview(imageBGView)
        
        scrollView.minimumZoomScale = 1.0
        scrollView.maximumZoomScale = 3.0
        scrollView.zoomScale = 1.0
        scrollView.bouncesZoom = false
        scrollView.contentSize = bSize
        scrollView.bounces = false
        
        let size = image.size
        if size.width == 0 || size.height == 0 || bSize.height == 0 || bSize.width == 0 {
            return
        }
        let imageView = UIImageView(image: image)
        
        if ( size.width / size.height ) > (bSize.width / bSize.height) {
            //图片较宽
            let fakeHeight = bSize.width*size.height/size.width
            imageView.frame = CGRect(x: 0, y: (bSize.height-fakeHeight)*0.5, width: bSize.width, height: fakeHeight)
            imageRect.origin.y = imageView.frame.minY / bSize.height
            imageRect.size.height = imageView.frame.height / bSize.height
        }else{
            let fakeWidth = bSize.height*size.width/size.height
            imageView.frame = CGRect(x: (bSize.width-fakeWidth)*0.5, y: 0, width: fakeWidth, height: bSize.height)
            imageRect.origin.x = imageView.frame.minX / bSize.width
            imageRect.size.width = imageView.frame.width / bSize.width
        }
        imageBGView.addSubview(imageView)
        imageBGView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(onTap(tap:))))
    }
    
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return imageBGView
    }
    
    func scrollViewDidZoom(_ scrollView: UIScrollView) {
//        print("contentOffset",scrollView.contentOffset)
//        print("contentSize",scrollView.contentSize)
//        print("imageRect",imageRect)
        fitImage()
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
//        print("contentOffset",scrollView.contentOffset)
//        print("contentSize",scrollView.contentSize)
//        print("imageRect",imageRect)
        fitImage()
    }
    
    var enterFitImage : Bool = false
    func fitImage(){
        if enterFitImage {
            return
        }
        enterFitImage = true
        let contentSize = scrollView.contentSize
        let offset = scrollView.contentOffset
        let bounds = scrollView.bounds

        let topLimit = imageRect.minY * contentSize.height
        let bottomLimit = imageRect.maxY * contentSize.height
        
        if offset.y < topLimit {
            if offset.y+bounds.height > bottomLimit {
                scrollView.contentOffset.y = contentSize.height*0.5 - bounds.height*0.5
            }else{
                scrollView.contentOffset.y = topLimit
            }
        }else if offset.y+bounds.height > bottomLimit {
            if offset.y < topLimit {
                scrollView.contentOffset.y = contentSize.height*0.5 - bounds.height*0.5
            }else{
                scrollView.contentOffset.y = bottomLimit-bounds.height
            }
        }
        
        let leftLimit = imageRect.minX * contentSize.width
        let rightLimit = imageRect.maxX * contentSize.width
        if offset.x < leftLimit {
            if offset.x+bounds.width > rightLimit {
                scrollView.contentOffset.x = contentSize.width*0.5 - bounds.width*0.5
            }else{
                scrollView.contentOffset.x = leftLimit
            }
        }else if offset.x+bounds.width > rightLimit {
            if offset.x < leftLimit {
                scrollView.contentOffset.x = contentSize.width*0.5 - bounds.width*0.5
            }else{
                scrollView.contentOffset.x = rightLimit-bounds.width
            }
        }
        enterFitImage = false
    }
    
    @objc func onTap(tap:UITapGestureRecognizer){
        self.removeFromSuperview()
    }
    
//    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
//        let p = convert(point, to: scrollView)
//        return scrollView.point(inside:p, with:event)
//    }
    
    static func showImage(vc:BaseViewController, image:UIImage?){
        if image != nil {
            _ = ImageViewer(vc: vc, image: image!)
        }else{
//            Toast.showAtTop(message: "请等待图片加载完成")
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
